//获取应用实例
var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {
            name: '',
            nickname: '',
            mobile: '',
        },
        avatarUrl: '',
        gradeIndex: 0,
        grades: ['请选择年级', '一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '初一', '初二', '初三', '高一', '高二', '高三']
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this
        that.getUserInfo()
        wx.getUserInfo({
            withCredentials: true,
            success: function(getUserInfoRes) {
                that.setData({
                    avatarUrl: getUserInfoRes.userInfo.avatarUrl
                })
            }
        })
    },
    //Picker
    gradeChange: function(e) {
        this.setData({
            gradeIndex: e.detail.value
        })
    },
    //获取我的信息
    getUserInfo: function() {
        let that = this
        let sign = wx.getStorageSync('sign')
        let data = {
            sign: sign
        }
        app.ajax({
            data: data,
            url: 'user/getUserInfo',
            method: 'GET'
        }).then(response => {
            if (response.data.code == 200) {
                var userInfo = response.data.data
                that.setData({
                    userInfo: {
                        name: userInfo.name,
                        mobile: userInfo.mobile,
                        nickname: userInfo.nickname
                    },
                    gradeIndex: userInfo.grade
                })
            } else {
                wx.showModal({
                    title: '温馨提示',
                    content: response.data.message,
                    showCancel: false
                })
            }
        })
    },
    //提交
    submitInfo: function(e) {
        var that = this
        var sign = wx.getStorageSync('sign')
        var nickname = e.detail.value.nickname
        var grade = e.detail.value.grade
        var data = {
            'sign': sign,
            'nickname': nickname,
            'grade': grade
        }
        app.ajax({
            data: data,
            url: 'user/update',
            method: 'POST'
        }).then(response => {
            if (response.data.code == 200) {
                wx.showToast({
                    title: '保存成功',
                })
            } else {
                wx.showModal({
                    title: '温馨提示',
                    content: response.data.message,
                    showCancel: false
                })
            }
        })
    },
    //退出登录
    logout(){
        wx.removeStorageSync('sign')
        wx.removeStorageSync('mobile')
        wx.reLaunch({
            url: '/pages/index/index',
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
})